package dbutils;

import exttable.timetable.ETestType;
import exttable.timetable.TimeTableSaver;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 24.11.2010
 * Time: 23:57:52
 * To change this template use File | Settings | File Templates.
 */
public class WeightSChemaGenerator {
    Connection con;
    private String analysis = null;
    private ETestType type;

    public WeightSChemaGenerator(int visitNumber, String analysisName, ETestType type) throws Exception {
        con = DbUtils.getDBConnectionGeneral();
        analysis = analysisName;
        this.type = type;
        createNewSchema(visitNumber);
    }

    private void createNewSchema(int visitNumber) throws SQLException {
        PreparedStatement ps = con.prepareStatement("INSERT INTO schema_visits (shema_visits) VALUES(?)");
        ps.setString(1, "Схема для заполнения побочных явлений ФС-1(2-ая фаза)");
        if (ps.executeUpdate() > 0) {
            createSchemaVisitTime(visitNumber);
        }

    }

    private void createSchemaVisitTime(int visitNumber) throws SQLException {
        int schemaVisitNumber = getMaxVisit("SELECT max(schema_visits_id) FROM schema_visits");
        int existsVisits = getMaxVisit("SELECT max(vizit_id) FROM dic_visits");

        if (existsVisits < visitNumber) {
            createNewVisits(visitNumber, existsVisits);
        }
        for (int i = 1; i <= visitNumber; i++) {
            long time=0;
            if(i==1|| i==2){
                time=0;
            }
            else{
             time = (i-2) * TimeTableSaver.DAYS_TO_SECONDS; 
            }
//            System.out.println("i : "+i+" time: "+time);
            PreparedStatement ps = con.prepareStatement("INSERT INTO schema_visit_time (schema_visits_id, visit_id, time_mls) VALUES (?, ?, ?)");
            ps.setInt(1, schemaVisitNumber);
            ps.setInt(2, i);
            ps.setLong(3, time);
            ps.executeUpdate();
        }
        groupsSchemaVisits(schemaVisitNumber, visitNumber);

    }

    private void createNewVisits(int visitNumber, int existsVisits) throws SQLException {

        for (int i = existsVisits + 1; i <= visitNumber; i++) {
            PreparedStatement ps = con.prepareStatement("INSERT INTO dic_visits(name_ru, name_en, name_kk) VALUES(?,?,?)");
            ps.setString(1, i + "- visit_ru");
            ps.setString(2, i + "- visit_en");
            ps.setString(3, i + "- visit_kk");
            ps.executeUpdate();
        }
    }

    private void groupsSchemaVisits(int schemaVisitNumber, int visitNumber) throws SQLException {
        PreparedStatement ps = con.prepareStatement("INSERT INTO groups_schema_visits (schema_visits_id, dic_test_type_id) VALUES(?, ?)");
        ps.setInt(1, schemaVisitNumber);
        ps.setString(2, type.name());
        if (ps.executeUpdate() > 0) {
            createTimeTable(visitNumber);
        }
    }

    private void createTimeTable(int visitNumber) throws SQLException {
        int maxGroupSchemaVisit = getMaxVisit("SELECT max(groups_schema_visits_id) FROM groups_schema_visits");
        int isCarriedOut=0;
        for (int i = 1; i <= visitNumber; i++) {
            if(i==1 || i==2){
              isCarriedOut=0;
            }
            else{
              isCarriedOut=1;  
            }
            PreparedStatement ps = con.prepareStatement("INSERT INTO timetable(groups_schema_visits_id, analysis_version_key, visit_id, is_carried_out) " +
                    " VALUES(?, ?, ?, ?)");
            ps.setInt(1, maxGroupSchemaVisit);
            ps.setString(2, analysis);
            ps.setInt(3, i);
            ps.setInt(4, isCarriedOut);
            ps.executeUpdate();
        }
//        System.out.println("OPERATION DONE");
    }

    private int getMaxVisit(String sqlStatement) throws SQLException {

        PreparedStatement ps = con.prepareStatement(sqlStatement);
        ResultSet rs = ps.executeQuery();
        rs.next();
        return rs.getInt(1);
    }


    public static void main(String[] args) throws Exception {
        new WeightSChemaGenerator(182, "A_039_VERSION_1", ETestType.SIDE_EFFECTS);
    }
}
